<template>
  <div id="flight-fuel-surcharge">
    <div class="card">
      <div class="card-header bg-info text-white">燃油附加费</div>
      <div class="card-body">
        <form class="row">
          <div class="col">
            <button
              type="button"
              class="btn btn-primary ms-1"
              @click.stop="search()"
            >
              查找
            </button>

            <a
              href="javascript:void(0)"
              class="btn btn-success float-end"
              @click="newFuelSurcharge()"
              >新建</a
            >
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-sm small">
      <thead>
        <tr>
          <th>里程类型</th>
          <th>生效日期</th>
          <th>燃油附加费</th>
          <th>保存时间</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr v-for="(info, index) in dataList">
          <td>{{ showMileageType(info.mileageType) }}</td>
          <td>{{ info.effectDate }}</td>
          <td>{{ info.surcharge }}</td>
          <td>{{ info.createTime }}</td>

          <td>
            <a
              href="javascript:void(0)"
              v-on:click="deleteFuelSurcharge(info.id)"
              >删除</a
            >
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <my-modal-fuel-surcharge ref="fuelSurchargeModal"></my-modal-fuel-surcharge>
  </div>
</template>

<script>
import MyPagination from "@/components/my-pagination.vue";
import MyDatePicker from "@/components/my-datepicker.vue";
import $ from "jquery";
import {
  searchFuelSurcharge,
  createFuelSurcharge,
  deleteFuelSurcharge,
} from "@/api/basic-data.js";
import MyModalFuelSurcharge from "@/components/modal/my-modal-fuel-surcharge.vue";

export default {
  components: {
    MyPagination,
    MyDatePicker,
    MyModalFuelSurcharge,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },

      dataList: [],
    };
  },
  mounted: function () {
    this.search();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
      };
      searchFuelSurcharge(params, (v) => {
        this.dataList = v.dataList;
        this.sc = v.page;
      });
    },

    newFuelSurcharge: function () {
      this.$refs.fuelSurchargeModal.modal().then((v) => {
        this.saveFuelSurcharge(v);
      });
    },
    saveFuelSurcharge: function (params) {
      createFuelSurcharge(params, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("操作成功");
          this.search();
        } else {
          this.showErrMsg("操作失败：" + v.errmsg, "danger");
        }
      });
    },
    deleteFuelSurcharge: function (id) {
      deleteFuelSurcharge(id, (v) => {
        if (v.status === "OK") {
          this.showErrMsg("删除成功");
          this.search();
        } else {
          this.showErrMsg(v.errmsg);
        }
      });
    },
    showMileageType: function (val) {
      if (val === 0) {
        return "小于等于800公里";
      } else if (val === 1) {
        return "大于800公里";
      }
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
  },
};
</script>
